/*package main

import (
	"fmt"
	"runtime"
)

func main()  {
	fmt.Println(runtime.NumCPU()) // 4
	fmt.Println(runtime.GOROOT()) // /home/haima/local/go
	fmt.Println(runtime.GOOS) // linux
	fmt.Println(runtime.Version()) // go1.17.2
}*/
/*
runtime.Gosched()
让出CPU时间片，重新等待安排任务
*/
//例子一
/*package main

import (
	"fmt"
	"runtime"
)

func show(msg string) {
	for i := 0; i < 3; i++ {
		fmt.Printf("msg: %v i:%v\n", msg, i)
	}
}

func main() {
	go show("java") // 子协程来运行
	for i := 0; i < 2; i++ {
		runtime.Gosched() // 我有权利执行任务了，让给你（其他子协程来执行）
		fmt.Printf("\"golang%v\": %v\n", i, "golang")
	}
	fmt.Println("end...")
}*/

//例子二
/*package main

import (
	"fmt"
	"runtime"
)

func showNumber(i int)  {
	fmt.Println(i)
}

func main()  {
	for i := 0; i < 10; i++ {
		go showNumber(i)
	}
	runtime.Gosched()
	fmt.Println("end ...")
}*/

/*
修改cpu的核心数
runtime.GOMAXPROCS
*/

/*package main

import (
	"fmt"
	"runtime"
	"time"
)

func a() {
	for i := 0; i < 10; i++ {
		fmt.Println("a:", i)
		time.Sleep(time.Millisecond * 100)
	}
}

func b() {
	for i := 0; i < 10; i++ {
		fmt.Println("b:", i)
		time.Sleep(time.Millisecond * 100)
	}
}

func main() {
	fmt.Printf("runtime.NumCPU(): %v\n", runtime.NumCPU())
	runtime.GOMAXPROCS(2) // 修改为1查看效果
	go a()
	go b()

	time.Sleep(time.Second)

}*/


/*
runtime.Goexit()
退出当前协程
*/
/*package main

import (
	"fmt"
	"runtime"
	"time"
)

func show() {
	for i := 0; i < 10; i++ {
		fmt.Printf("i: %v\n", i)
		if i >= 5 {
			runtime.Goexit()
		}
	}
}

func main() {
	go show()
	time.Sleep(time.Second)
}*/


